﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RiskMan.Config;
using RiskMan.DataClass.Models;
using RiskMan.DataClass.Models.RM;
using RiskMan.Views.Dialogs;

namespace RiskMan.Views.Stop
{
    /// <summary>
    /// Логика взаимодействия для newStItem.xaml
    /// </summary>
    public partial class newStItem : Window
    {
        public StopListModel stop;
        private DataClass.Data data;

        public newStItem ( StopListModel stopListModel, DataClass.Data data, bool isEdit )
        {
            InitializeComponent( );

            this.stop = stopListModel;
            this.data = data;

            List<Trade> trades = data.GetTradeList( );
            
            List<String> seccode = data.GetSeccodeList( );

            #region ClientCode

            ComboBox_ClientCode.ItemsSource = trades.Select( a => a.ClientCode + "_" + a.Account );
            for ( int i = 0; i < ComboBox_ClientCode.Items.Count; i++ )
            {
                if ( ComboBox_ClientCode.Items [ i ].ToString( ) ==
                    stopListModel.ClientCode + "_" + stopListModel.Account )
                    ComboBox_ClientCode.SelectedIndex = i;
            }

            #endregion

            #region Seccode

            ComboBox_Seccode.ItemsSource = data.GetSeccodeList( );
            for ( int i = 0; i < ComboBox_Seccode.Items.Count; i++ )
            {
                if ( ComboBox_Seccode.Items [ i ].ToString( ) == stopListModel.Seccode )
                    ComboBox_Seccode.SelectedIndex = i;
            }

            #endregion

            #region TypeRaschet

            ComboBox_TypeRasch.ItemsSource = stop.TypeRaschList;
            for ( int i = 0; i < stop.TypeRaschList.Count; i++ )
            {
                if ( stop.TypeRaschList [ i ].ToString( ) == stop.TypeRasch )
                    ComboBox_TypeRasch.SelectedIndex = i;
            }

            #endregion

            #region TypeStop

            ComboBox_TypeStop.ItemsSource = stop.TypeStopList;
            for ( int i = 0; i < stop.TypeStopList.Count; i++ )
            {
                if ( stop.TypeStopList [ i ].ToString( ) == stop.TypeStop )
                    ComboBox_TypeStop.SelectedIndex = i;
            }

            #endregion

            TextBox_StopLimitOtst.Text = stop.StopOtst.ToString( );

            TextBox_StopLimitSpread.Text = stop.StopSprd.ToString( );
            TextBox_TakeProfitOtst.Text = stop.TakeOtst.ToString( );
            TextBox_TakeProfitSpread.Text = stop.TakeSprd.ToString( );
            TextBox_PriceActivated.Text = stop.TakePrAc.ToString( );
            txbxToTransferBezubitok.Text = stop.Lossless.ToString();
        }

        #region Кнопка "Отмена"

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Кнопка "Применить"

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBox_ClientCode.SelectedIndex >= 0)
            {
                List<Trade> list = data.GetTradeList();
                foreach (var tmp in list)
                {
                    string[] trade = ComboBox_ClientCode.SelectedItem.ToString().Split('_');

                    if (tmp.Account == trade[1] && tmp.ClientCode == trade[0])
                    {
                        stop.ClientCode = tmp.ClientCode;
                        stop.Account = tmp.Account;
                    }
                }
            }


            if (ComboBox_Seccode.SelectedIndex >= 1)
                stop.Seccode = ComboBox_Seccode.SelectedItem.ToString();
            else
            {
                new DialogOkCancel( "Выберите Код Бумаги!", "Ошибка применения параметров" );
                return;
            }

            if (ComboBox_TypeStop.SelectedIndex >= 1)
                stop.TypeStop = ComboBox_TypeStop.SelectedItem.ToString();
            else
            {
                new DialogOkCancel( "Выберите тип стоп заявки!", "Ошибка применения параметров" );
                return;
            }

            if (ComboBox_TypeRasch.SelectedIndex >= 1)
                stop.TypeRasch = ComboBox_TypeRasch.SelectedItem.ToString();
            else
            {
                new DialogOkCancel( "Выберите тип расчета цен для стоп заявок!", "Ошибка применения параметров" );
                return;
            }

            #region stop limit otstup
            try
            {
                stop.StopOtst = cfg.ToDoubleFormControls(TextBox_StopLimitOtst.Text);
            }
            catch (Exception)
            {
                new DialogOkCancel( "Введите Стоп-Лимит отступ корректно!", "Ошибка применения параметров" );
                return;
            }
            #endregion

            #region stop limit spread
            try
            {
                stop.StopSprd = cfg.ToDoubleFormControls(TextBox_StopLimitSpread.Text);
            }
            catch (Exception)
            {
                new DialogOkCancel( "Введите Стоп-Лимит спред корректно!", "Ошибка применения параметров" );
                return;
            }
            #endregion

            #region take limit отсуп
            try
            {
                stop.TakeOtst = cfg.ToDoubleFormControls(TextBox_TakeProfitOtst.Text);
            }
            catch (Exception)
            {
                new DialogOkCancel( "Введите Тейк-профит отступ корректно!", "Ошибка применения параметров" );
                return;
            }
            #endregion

            #region lossless
            try
            {
                stop.Lossless = cfg.ToDoubleFormControls(txbxToTransferBezubitok.Text);
            }
            catch ( Exception )
            {
                new DialogOkCancel( "Введите значение переноса в безубыток корректно!", "Ошибка применения параметров" );
                return;
            }
            #endregion

            #region take limit spread
            try
            {
                stop.TakeSprd = cfg.ToDoubleFormControls(TextBox_TakeProfitSpread.Text);
            }
            catch (Exception)
            {
                new DialogOkCancel( "Введите Тейк-профит спред корректно!", "Ошибка применения параметров" );
                return;
            }
            #endregion

            #region take limit price activate
            try
            {
                stop.TakePrAc = cfg.ToDoubleFormControls(TextBox_PriceActivated.Text);
            }
            catch (Exception)
            {
                new DialogOkCancel( "Введите цену активации!", "Ошибка применения параметров" );
                return;
            }
            #endregion

            this.Close();
        }

        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void ComboBox_TypeStop_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (((ComboBox)sender).SelectedValue.ToString())
            {
                case "Стоп-лимит":
                    TextBox_StopLimitOtst.IsEnabled = true;
                    TextBox_StopLimitSpread.IsEnabled = true;
                    TextBox_TakeProfitOtst.IsEnabled = false;
                    TextBox_TakeProfitSpread.IsEnabled = false;
                    TextBox_PriceActivated.IsEnabled = false;
                    txbxToTransferBezubitok.IsEnabled = true;
                    break;
                case "Тэйк-профит":
                    TextBox_StopLimitOtst.IsEnabled = false;
                    TextBox_StopLimitSpread.IsEnabled = false;
                    TextBox_TakeProfitOtst.IsEnabled = true;
                    TextBox_TakeProfitSpread.IsEnabled = true;
                    TextBox_PriceActivated.IsEnabled = true;
                    txbxToTransferBezubitok.IsEnabled = false;
                    break;
                case "Связанная заявка":
                    TextBox_StopLimitOtst.IsEnabled = true;
                    TextBox_StopLimitSpread.IsEnabled = true;
                    TextBox_TakeProfitOtst.IsEnabled = false;
                    TextBox_TakeProfitSpread.IsEnabled = false;
                    TextBox_PriceActivated.IsEnabled = true;
                    txbxToTransferBezubitok.IsEnabled = true;
                    break;
                case "Тэйк-профит и стоп-лимит":
                    TextBox_StopLimitOtst.IsEnabled = true;
                    TextBox_StopLimitSpread.IsEnabled = true;
                    TextBox_TakeProfitOtst.IsEnabled = true;
                    TextBox_TakeProfitSpread.IsEnabled = true;
                    TextBox_PriceActivated.IsEnabled = true;
                    txbxToTransferBezubitok.IsEnabled = true;
                    break;
                case "Тейк-профит(Лимит. заявка)":
                    TextBox_StopLimitOtst.IsEnabled = false;
                    TextBox_StopLimitSpread.IsEnabled = false;
                    TextBox_TakeProfitOtst.IsEnabled = false;
                    TextBox_TakeProfitSpread.IsEnabled = false;
                    TextBox_PriceActivated.IsEnabled = true;
                    txbxToTransferBezubitok.IsEnabled = false;
                    break;
                default:
                    break;

            }
        }

        
    }
}
